{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-07T12:59:14.293552Z",
     "start_time": "2025-01-07T12:59:14.289754Z"
    }
   },
   "source": [
    "import numpy as np\n",
    "#nan 是not a number的缩写，表示非数字，在numpy中表示一个非数字值，可以用来表示缺失数据\n",
    "a = np.nan\n",
    "b = np.inf\n",
    "print(a, type(a))\n",
    "print(b, type(b))"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nan <class 'float'>\n",
      "inf <class 'float'>\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T12:59:14.302162Z",
     "start_time": "2025-01-07T12:59:14.297541Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#nan和其他数运算的结果都是nan\n",
    "print(np.nan == np.nan)\n",
    "print(True == 1)\n",
    "print(False == 0)\n",
    "np.nan + 1 \n"
   ],
   "id": "1d5d6ce10889bf50",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n",
      "True\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "nan"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T12:59:14.323033Z",
     "start_time": "2025-01-07T12:59:14.319113Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#判断数组中为nan的个数\n",
    "t = np.arange(24, dtype=float).reshape(4, 6)\n",
    "\n",
    "# 将三行四列的数改成nan\n",
    "t[3, 4] = np.nan\n",
    "t[2, 4] = np.nan\n",
    "print(t)"
   ],
   "id": "d94905a0bf0c69ce",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15. nan 17.]\n",
      " [18. 19. 20. 21. nan 23.]]\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T12:59:14.326960Z",
     "start_time": "2025-01-07T12:59:14.324031Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 可以使用np.count_nonzero() 来判断非零的个数\n",
    "# print(np.count_nonzero(t))\n",
    "print(t != t)\n",
    "print('-' * 50)\n",
    "print(np.count_nonzero(t != t))  # 统计nan的个数"
   ],
   "id": "cdb9ec3564e81b2e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[False False False False False False]\n",
      " [False False False False False False]\n",
      " [False False False False  True False]\n",
      " [False False False False  True False]]\n",
      "--------------------------------------------------\n",
      "2\n"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T12:59:14.332706Z",
     "start_time": "2025-01-07T12:59:14.329954Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 将nan替换为0\n",
    "t[np.isnan(t)] = 0\n",
    "print(t)"
   ],
   "id": "941ab636b95f0618",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15.  0. 17.]\n",
      " [18. 19. 20. 21.  0. 23.]]\n"
     ]
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T12:59:14.337638Z",
     "start_time": "2025-01-07T12:59:14.332706Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "# ----------练习： 处理数组中nan\n",
    "t = np.arange(24).reshape(4, 6).astype('float')\n",
    "#\n",
    "# 将数组中的一部分替换nan\n",
    "t[1, 3:] = np.nan\n",
    "print(t)\n",
    "print('-------------------')\n",
    "print(t.shape)\n",
    "print(id(t))\n",
    "t.shape[1] "
   ],
   "id": "2eb74d852710c8aa",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8. nan nan nan]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n",
      "-------------------\n",
      "(4, 6)\n",
      "1914579613328\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T12:59:14.342884Z",
     "start_time": "2025-01-07T12:59:14.338640Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 遍历每一列，然后判断每一列是否有nan\n",
    "for i in range(t.shape[1]):\n",
    "    #获取当前列数据\n",
    "    temp_col = t[:, i]\n",
    "\n",
    "    # 判断当前列的数据中是否含有nan\n",
    "    nan_num = np.count_nonzero(temp_col != temp_col)\n",
    "    # 条件成立说明含有nan\n",
    "    if nan_num != 0:\n",
    "        # 将这一列不为nan的数据拿出来,并计算平均值\n",
    "        temp_col_not_nan = temp_col[temp_col == temp_col]\n",
    "        print(temp_col_not_nan)\n",
    "        # 将nan替换成这一列的平均值\n",
    "        temp_col[np.isnan(temp_col)] = np.mean(temp_col_not_nan)\n",
    "print('-'*50)\n",
    "print(t)\n",
    "print(id(t))"
   ],
   "id": "aacef01f07834eee",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 3. 15. 21.]\n",
      "[ 4. 16. 22.]\n",
      "[ 5. 17. 23.]\n",
      "--------------------------------------------------\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8. 13. 14. 15.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n",
      "1914579613328\n"
     ]
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T12:59:14.346739Z",
     "start_time": "2025-01-07T12:59:14.343883Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(np.inf == np.inf)\n",
    "np.inf"
   ],
   "id": "57c5c93b58185bfc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "inf"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T12:59:14.350690Z",
     "start_time": "2025-01-07T12:59:14.347738Z"
    }
   },
   "cell_type": "code",
   "source": "np.inf >-np.inf",
   "id": "c4d90f991c209716",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T12:59:14.356002Z",
     "start_time": "2025-01-07T12:59:14.350690Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# np.nan和任何数据运算的结果都是nan\n",
    "t = np.arange(24).reshape(4, 6).astype('float')\n",
    "#\n",
    "# 将数组中的一部分替换nan\n",
    "t[1, 3:] = np.nan\n",
    "\n",
    "t1 = np.arange(24).reshape(4, 6).astype('float')\n",
    "t + t1"
   ],
   "id": "2ee398b4f9e5cbee",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.,  2.,  4.,  6.,  8., 10.],\n",
       "       [12., 14., 16., nan, nan, nan],\n",
       "       [24., 26., 28., 30., 32., 34.],\n",
       "       [36., 38., 40., 42., 44., 46.]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T12:59:14.362156Z",
     "start_time": "2025-01-07T12:59:14.357007Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array([-1, 0])\n",
    "print(arr)\n",
    "print(arr[0] / arr[1])  #1除0就会得到inf"
   ],
   "id": "db0ea25337ceb0f5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-1  0]\n",
      "-inf\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\MECHREV\\AppData\\Local\\Temp\\ipykernel_5172\\773640058.py:3: RuntimeWarning: divide by zero encountered in scalar divide\n",
      "  print(arr[0] / arr[1])  #1除0就会得到inf\n"
     ]
    }
   ],
   "execution_count": 31
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T12:59:14.374990Z",
     "start_time": "2025-01-07T12:59:14.372128Z"
    }
   },
   "cell_type": "code",
   "source": "np.nan + np.inf",
   "id": "45548f6b971f905e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "nan"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:02:22.258351Z",
     "start_time": "2025-01-07T13:02:22.253693Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.arange(12).reshape(3,4)#创建3行4列的数组\n",
    "print('原数组：')\n",
    "print(a)\n",
    "print('\\n')\n",
    "print('对换数组：')\n",
    "print(np.transpose(a))#对换数组的行列\n",
    "print(a)\n",
    "\n",
    "#与transpose()函数的作用相同，但是transpose()函数是对原数组进行转置，而T属性则是对原数组的视图进行转置。\n",
    "a = np.arange(12).reshape(3, 4)\n",
    "\n",
    "print('原数组：')\n",
    "print(a)\n",
    "print('\\n')\n",
    "\n",
    "print('转置数组：')\n",
    "print(a.T)\n"
   ],
   "id": "10951666ff9ad767",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "\n",
      "对换数组：\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "原数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "\n",
      "转置数组：\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n"
     ]
    }
   ],
   "execution_count": 36
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:02:33.799527Z",
     "start_time": "2025-01-07T13:02:33.796007Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 函数用于交换数组的两个轴\n",
    "t1 = np.arange(24).reshape(4, 6)\n",
    "re1 = t1.swapaxes(1, 0)\n",
    "\n",
    "print(' 原 数 组 ：')\n",
    "print(t1)\n",
    "print('\\n')\n",
    "print(re1.shape)\n",
    "print('调用 swapaxes 函数后的数组：')\n",
    "print(re1)\n"
   ],
   "id": "2059e22955b034c3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 原 数 组 ：\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "\n",
      "\n",
      "(6, 4)\n",
      "调用 swapaxes 函数后的数组：\n",
      "[[ 0  6 12 18]\n",
      " [ 1  7 13 19]\n",
      " [ 2  8 14 20]\n",
      " [ 3  9 15 21]\n",
      " [ 4 10 16 22]\n",
      " [ 5 11 17 23]]\n"
     ]
    }
   ],
   "execution_count": 37
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:02:45.737035Z",
     "start_time": "2025-01-07T13:02:45.733265Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#如果是3维及以上，称为轴交换\n",
    "t3 = np.arange(60).reshape(3, 4, 5)#创建3维数组\n",
    "print(t3.shape)\n",
    "print('-' * 50)\n",
    "t3 = np.swapaxes(t3, 1, 2)#将第2轴和第3轴交换\n",
    "print(t3.shape)\n",
    "# print(t3) 数据不用记住，不用观察"
   ],
   "id": "1e0f1366cc466346",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 4, 5)\n",
      "--------------------------------------------------\n",
      "(3, 5, 4)\n"
     ]
    }
   ],
   "execution_count": 38
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:03:19.370465Z",
     "start_time": "2025-01-07T13:03:19.366546Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 数组的轴滚动,swapaxes每次只能交换两个轴，没有rollaxis方便，默认情况下轴滚动最前面\n",
    "a = np.ones((3, 4, 5, 6))#创建4维数组\n",
    "# np.rollaxis(a, 2).shape\n",
    "b = np.rollaxis(a, 3, 1)#将第3轴滚动到第1轴\n",
    "print(b.shape)"
   ],
   "id": "258bf302d4f40992",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 6, 4, 5)\n"
     ]
    }
   ],
   "execution_count": 39
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:03:26.314535Z",
     "start_time": "2025-01-07T13:03:26.311226Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(b.shape)\n",
    "c=np.rollaxis(b, 1,4)  #轴会滚动，直到它位于此位置之前\n",
    "print(c.shape)"
   ],
   "id": "d8e02f3a880642de",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 6, 4, 5)\n",
      "(3, 4, 5, 6)\n"
     ]
    }
   ],
   "execution_count": 40
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:03:33.059386Z",
     "start_time": "2025-01-07T13:03:33.055895Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#数据拷贝，copy()和赋值的区别\n",
    "b = np.array([[1, 2, 3], [1, 2, 3]])\n",
    "a = b.copy()\n",
    "a"
   ],
   "id": "34f09de975d294a7",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [1, 2, 3]])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 41
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:03:51.779275Z",
     "start_time": "2025-01-07T13:03:51.774678Z"
    }
   },
   "cell_type": "code",
   "source": [
    "b[0, 0] = 3\n",
    "print(b)\n",
    "a"
   ],
   "id": "2a466a67eeaddc15",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3 2 3]\n",
      " [1 2 3]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [1, 2, 3]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 42
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:04:08.501763Z",
     "start_time": "2025-01-07T13:04:08.498005Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#随机数生成\n",
    "arr = np.random.rand(2, 3, 4)#生成2*3*4的随机数组\n",
    "print(arr)"
   ],
   "id": "8671080302f36bf4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[0.06587812 0.77436249 0.46649767 0.95267606]\n",
      "  [0.88875685 0.83854687 0.6918536  0.83796877]\n",
      "  [0.06282503 0.79693949 0.72363562 0.1339338 ]]\n",
      "\n",
      " [[0.92723425 0.33002777 0.94111058 0.46370222]\n",
      "  [0.55900268 0.04181918 0.70397169 0.14030406]\n",
      "  [0.57419371 0.47776049 0.94563254 0.41486384]]]\n"
     ]
    }
   ],
   "execution_count": 43
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:04:19.249193Z",
     "start_time": "2025-01-07T13:04:19.245280Z"
    }
   },
   "cell_type": "code",
   "source": [
    "names = 'zhangsan,lisi,wangwu,zhaoliu,sunqi'\n",
    "#写入文件\n",
    "with open('names.csv', 'w') as f:\n",
    "    f.write(names)\n",
    "    f.write('\\n')\n",
    "    f.write('12,23,34,45,56')"
   ],
   "id": "a2ed1a5967c829af",
   "outputs": [],
   "execution_count": 44
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
